package com.edgclub.edg.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.edgclub.edg.R;
import com.edgclub.edg.adapter.FansMembersAdapter;
import com.edgclub.edg.api.FriendApi;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.param.MessageParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.model.User;
import com.edgclub.edg.model.result.FansBeanData;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.JsonUtil;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;


/**
 * 支付管理
 */
public class FansMembersActivity extends GzBaseTooolBarActivity implements
        OnClickListener ,VolleyListener{

    private XRecyclerView recyclerView;
    private int currPage = 1;
    private int pageSize = 20;
    private FansMembersAdapter fansMembersAdapter;
    private UserPrefUtils userPrefUtils;
    private String groupid;
    private List<User>userList;
    private TextView text_empty;
    private ImageView imageView_empty;
    private LinearLayout layout_empty;
    @Override
    public int getContentView() {
        return R.layout.activity_group_people  ;
    }
    @Override
    public void setTitleBarView() {
        setCustomActionBar();
        title.setText("我的粉丝");
        main_back.setVisibility(View.VISIBLE);
        main_back.setOnClickListener(this);
    }
    @Override
    public void initView() {
        recyclerView= (XRecyclerView) findViewById(R.id.recyclerview);
        layout_empty= (LinearLayout) findViewById(R.id.layout_empty);;
        text_empty= (TextView) findViewById(R.id.text_empty);;
        imageView_empty = (ImageView) findViewById(R.id.imageView_empty);
        imageView_empty.setVisibility(View.VISIBLE);
        imageView_empty.setImageResource(R.drawable.nomess);
        text_empty.setText("你还没被人关注哦");
    }
    @Override
    public void initData() {
        showDialog1();
        userPrefUtils = new UserPrefUtils(this);
        groupid=getIntent().getStringExtra("groupid");
        fansMembersAdapter= new FansMembersAdapter(this);
        userList=new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        fansMembersAdapter.setUsers(userList);
        recyclerView.setAdapter(fansMembersAdapter);

        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                currPage = 1;
                getmembers();
            }

            @Override
            public void onLoadMore() {
                getmembers();
            }
        });
        getmembers();
    }

    private void getmembers(){
        MessageParam messageParam=new MessageParam();
        messageParam.setUserId(userPrefUtils.getUser().getId() + "");
        messageParam.setPageSize(pageSize+"");
        messageParam.setPage(currPage+"");
        new FriendApi(this).getmyFans(messageParam,this);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.main_back:
                finish();
            break;
            default:
                break;
        }
    }


    @Override
    public void onStart(String tag) {

    }

    @Override
    public void onSuccess(String response, String tag) {
        dismissDialog();
        recyclerView.refreshComplete();
        recyclerView.loadMoreComplete();
        if (JSONHelper.isSuccess(response)) {
            FansBeanData user = JsonUtil.getMode(response,
                    FansBeanData.class);
            if(null!=user&&null!=user.getData().getUsers() && user.getData().getUsers().size()>0){
                if(currPage==1){
                    userList.clear();
                }
                currPage++;

                userList.addAll(user.getData().getUsers());
                fansMembersAdapter.notifyDataSetChanged();
            }else{
                recyclerView.noMoreLoading();
            }
        }
        if (!"".equals(userList)&&userList.size()>0){
            layout_empty.setVisibility(View.GONE);
            recyclerView.setPullRefreshEnabled(true);
        }
        else {
            layout_empty.setVisibility(View.VISIBLE);
            recyclerView.setPullRefreshEnabled(false);
        }
    }

    @Override
    public void onFaile(VolleyError volleyError, String tag) {
        setNoNetWork(new ReLoadClickListener() {
            @Override
            public void reload() {
                getmembers();
            }
        });
        recyclerView.refreshComplete();
        recyclerView.loadMoreComplete();
        ResultCode.toastVolleyError(this, volleyError);
    }
}
